package com.joker.aigc.chat.service.adapter;


import com.joker.aigc.domain.entity.User;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketBaseVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketInvitationVo;
import com.joker.aigc.chat.domain.entity.websocket.WebSocketMemberChangeVo;
import com.joker.aigc.chat.domain.enums.WebSocketVoTypeEnum;
import com.joker.aigc.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

import static com.joker.aigc.chat.domain.entity.websocket.WebSocketMemberChangeVo.CHANGE_TYPE_ADD;


/**
 * Description: 成员适配器
 * Author: <a href="https://github.com/zongzibinbin">abin</a>
 * Date: 2023-03-26
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class MemberAdapter {


    final UserService userCache;

//    public static List<ChatMemberResp> buildMember(List<User> list) {
//        return list.stream().map(a -> {
//            ChatMemberResp resp = new ChatMemberResp();
//            resp.setActiveStatus(a.getActiveStatus());
//            resp.setLastOptTime(a.getLastOptTime());
//            resp.setUid(a.getId());
//            return resp;
//        }).collect(Collectors.toList());
//    }

//    public static List<ChatMemberResp> buildMember(List<UserFriend> list, List<User> userList) {
//        Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
//        return list.stream().map(userFriend -> {
//            ChatMemberResp resp = new ChatMemberResp();
//            resp.setUid(userFriend.getFriendUid());
//            User user = userMap.get(userFriend.getFriendUid());
//            if (Objects.nonNull(user)) {
//                resp.setActiveStatus(user.getActiveStatus());
//                resp.setLastOptTime(user.getLastOptTime());
//            }
//            return resp;
//        }).collect(Collectors.toList());
//    }


//    public static List<ChatMemberListResp> buildMemberList(List<User> memberList) {
//        return memberList.stream()
//                .map(a -> {
//                    ChatMemberListResp resp = new ChatMemberListResp();
//                    BeanUtils.copyProperties(a, resp);
//                    resp.setUid(a.getId());
//                    return resp;
//                }).collect(Collectors.toList());
//    }

//    public static List<ChatMemberListResp> buildMemberList(Map<Long, User> batch) {
//        return buildMemberList(new ArrayList<>(batch.values()));
//    }

//    public static List<GroupMember> buildMemberAdd(Long groupId, List<Long> waitAddUidList) {
//        return waitAddUidList.stream().map(a -> {
//            GroupMember member = new GroupMember();
//            member.setGroupId(groupId);
//            member.setUid(a);
//            member.setRole(GroupRoleEnum.MEMBER.getType());
//            return member;
//        }).collect(Collectors.toList());
//    }

    public static WebSocketBaseVo<WebSocketInvitationVo> buildGroupAddMessage(User inviter,List<User> member) {
        WebSocketBaseVo<WebSocketInvitationVo> baseVo = new WebSocketBaseVo<>();
        baseVo.setType(WebSocketVoTypeEnum.ROOM_INVITATION.getType());
        WebSocketInvitationVo webSocketInvitationVo = new WebSocketInvitationVo();
        StringBuilder sb = new StringBuilder();
        sb.append("\"")
                .append(inviter.getUsername())
                .append("\"")
                .append("邀请")
                .append(member.stream().map(u -> "\"" + u.getUsername() + "\"").collect(Collectors.joining(",")))
                .append("加入群聊");
        webSocketInvitationVo.setMsg(sb.toString());
        baseVo.setData(webSocketInvitationVo);
        return baseVo;
    }

    public static WebSocketBaseVo<WebSocketMemberChangeVo> buildMemberAddWebSocket(Long roomId, User user) {
        WebSocketBaseVo<WebSocketMemberChangeVo> wsBaseResp = new WebSocketBaseVo<>();
        wsBaseResp.setType(WebSocketVoTypeEnum.MEMBER_CHANGE.getType());
        WebSocketMemberChangeVo wsMemberChange = new WebSocketMemberChangeVo();
        wsMemberChange.setActiveStatus(user.getActiveStatus());
        wsMemberChange.setLastOptTime(user.getLastOptTime());
        wsMemberChange.setUid(user.getId());
        wsMemberChange.setRoomId(roomId);
        wsMemberChange.setChangeType(CHANGE_TYPE_ADD);
        wsBaseResp.setData(wsMemberChange);
        return wsBaseResp;
    }

//    public static WSBaseResp<WSMemberChange> buildMemberRemoveWS(Long roomId, Long uid) {
//        WSBaseResp<WSMemberChange> wsBaseResp = new WSBaseResp<>();
//        wsBaseResp.setType(WSRespTypeEnum.MEMBER_CHANGE.getType());
//        WSMemberChange wsMemberChange = new WSMemberChange();
//        wsMemberChange.setUid(uid);
//        wsMemberChange.setRoomId(roomId);
//        wsMemberChange.setChangeType(CHANGE_TYPE_REMOVE);
//        wsBaseResp.setData(wsMemberChange);
//        return wsBaseResp;
//    }
}
